<template>
	<view class="content mainpadding">
		<view class="flexbetween margin_bottom3 border ffffff mainpadding">
			<view class="color_ling text_w white_nowrap margin_right3">
				新手機號
			</view>
			<input class="flex1 textright text_a" type="text" placeholder="請輸入新手機號"
				placeholder-style="font-size: 30rpx;font-weight: 500;color: #999999;" v-model="mobile">
		</view>
		<view class="flexbetween margin_bottom3 border ffffff mainpadding">
			<view class="color_ling text_w white_nowrap margin_right3">
				驗證碼
			</view>
			<view class="flexleft">
				<input class="textright text_a" type="text" placeholder="請輸入驗證碼"
					placeholder-style="font-size: 30rpx;font-weight: 500;color: #999999;"  v-model="captcha">
				<view class="line margin_left2 margin_right2"></view>
				<view class="send" @click="sendcode" v-if="codetext=='獲取驗證碼'">{{codetext}}</view>
				<view class="send" v-if="codetext!='獲取驗證碼'">{{codetext}}</view>
			</view>
		</view>
		<!-- 底部 -->
		<view class="footer mainpadding">
			<view class="footerButton" @click="confirm">
				確認更換
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				codetext: "獲取驗證碼",
				mobile: "", //手機號
				captcha: "", //驗證碼
				codesure:"",
			}
		},
		methods: {
			sendcode() { // 發送驗證碼
				let _this = this
				if (!this.mobile) {
					this.http.toast("請輸入手機號！")
					return false
				}
				let mobile = _this.mobile;
				this.http.request('/api/sms/fs', 'POST', {
					mobile: mobile,
					event:"changemobile"
				}).then(resp => {
					if (resp.code == 1) {
						var i = 60;
						var timer = setInterval(function() {
							_this.codetext = i + "秒";
							if (i < 1) {
								clearInterval(timer);
								_this.codetext = "獲取驗證碼";
							} else {
								i--;
							}
						}, 1000)
						this.codesure = res.data
					} else if (resp.code == 0) {
						this.http.toast(resp.msg);
					} else {
						this.http.toast('系統處理中，請稍後再試');
					}
				})
			},
			async confirm() { //確認更換
				if (!this.mobile) {
					this.http.toast("手機號不能爲空！")
					return false
				}
				if (!this.captcha) {
					this.http.toast("驗證碼不能爲空！")
					return false
				}
				let res = await this.http.request('/api/user/changemobile', 'POST', {
					mobile: this.mobile,
					captcha: this.captcha
				})
				if (res.code == 1) {
					this.http.toast("修改成功");
					setTimeout(() => {
						uni.navigateBack(1)
					}, 500)
				} else if (res.code == 0) {
					this.http.toast(res.msg);
				} else {
					this.http.toast('系統處理中，請稍後再試');
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.line {
		width: 0;
		height: 20rpx;
		border-right: 1rpx solid #999999;
	}

	.send {
		font-size: 30rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #FFB800;
		margin-right: 14rpx;
		min-width: 150rpx;
		text-align: right;
	}
</style>